package control;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.MD5;
import model.User;
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
	public Register() {
		super();
	}
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
	}
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");
		String first_name = request.getParameter("first_name");
		String last_name = request.getParameter("last_name");
		String userName = request.getParameter("user_name");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String r_password = request.getParameter("r_password");
		String password1 = "";
		if(password!=null){
			try {
				password1 = MD5.encode(password);
			} catch (NoSuchAlgorithmException e) {
				System.out.println("Mã hóa MD5 thất bại");
			}
		}
		String active = String.valueOf(Math.random()*100000000);
		try {
			active = MD5.encode(active);
		} catch (NoSuchAlgorithmException e) {
			System.out.println("Mã hóa MD5 thất bại");
		}
		User user = new User("", first_name, last_name, userName, email, password1, "", "", "", "", "1", active, null);
		boolean checkuser = user.chexUser();
		System.out.println(checkuser);
		boolean checkPass = user.checkIssetPass(password,r_password);
		System.out.println(checkPass);
		if (!(checkuser && checkPass)) {
			request.setAttribute("fn", first_name);
			request.setAttribute("ln", last_name);
			request.setAttribute("un", userName);
			request.setAttribute("e", email);
			request.setAttribute("p", password);
			request.setAttribute("rp", r_password);
			request.setAttribute("efn", user.checkFirstName());
			request.setAttribute("eln", user.checkLastName());
			request.setAttribute("eun", user.checkUserName());
			request.setAttribute("ee", user.checkEmail());
			request.setAttribute("ep", user.checkPass(password));
			request.setAttribute("erp", user.checkRePass(password, r_password));
			RequestDispatcher transferexist = getServletContext().getRequestDispatcher("/register.jsp");
			transferexist.forward(request, response);
		} else {
			user.Register();
			// response.sendRedirect("success.jsp");
			request.setAttribute("user", userName);
			request.setAttribute("pass", password1);
			RequestDispatcher transfer = getServletContext().getRequestDispatcher("/index.jsp");
			transfer.forward(request, response);
		}
	}

}
